package mq.rabbitmq.d_topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import mq.rabbitmq.ConnectionUtil;
import mq.rabbitmq.MqConstants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 通配符模式生产者
 * Topic与Routing模式相比，都是根据routing key把消息路由到不同的队列，
 * 只不过Topic类型Exchange可以让队列在绑定routing key的时候使用通配符
 * 通配符规则：# 匹配一个或多个词; * 匹配不多不少恰好1个词
 *
 * @author shanzhenbo
 */
public class Producer {

	public static void main(String[] args) throws IOException, TimeoutException {
		Connection connection = ConnectionUtil.getConnection();
		Channel channel = connection.createChannel();

		channel.exchangeDeclare(MqConstants.TOPIC_EXCHANGE, BuiltinExchangeType.TOPIC);

		// 发送信息
		String message = "新增了商品。Topic模式；routing key 为 item.insert ";
		channel.basicPublish(MqConstants.TOPIC_EXCHANGE, "item.insert", null,
				message.getBytes());
		System.out.println("已发送消息：" + message);

		// 发送信息
		message = "修改了商品。Topic模式；routing key 为 item.update";
		channel.basicPublish(MqConstants.TOPIC_EXCHANGE, "item.update", null,
				message.getBytes());
		System.out.println("已发送消息：" + message);

		// 发送信息
		message = "删除了商品。Topic模式；routing key 为 item.delete";
		channel.basicPublish(MqConstants.TOPIC_EXCHANGE, "item.delete", null,
				message.getBytes());
		System.out.println("已发送消息：" + message);

		// 关闭资源
		channel.close();
		connection.close();
	}

}
